.TH std::bad_exception 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::bad_exception \- std::bad_exception

.SH Synopsis
   Defined in header <exception>
   class bad_exception;

   std::bad_exception is the type of the exception thrown by the C++ runtime in the
   following situations:

     * If std::exception_ptr stores a copy of the caught exception and if
       the copy constructor of the exception object caught by             \fI(since C++11)\fP
       std::current_exception throws an exception, the captured exception
       is an instance of std::bad_exception.
     * If a dynamic exception specification is violated and
       std::unexpected throws or rethrows an exception that still
       violates the exception specification, but the exception            \fI(until C++17)\fP
       specification allows std::bad_exception, std::bad_exception is
       thrown.

   std-bad exception-inheritance.svg

                                   Inheritance diagram

.SH Member functions

   constructor   constructs the bad_exception object
                 \fI(public member function)\fP
   operator=     copies the object
                 \fI(public member function)\fP
   what          returns the explanatory string
   \fB[virtual]\fP     \fI(virtual public member function)\fP

Inherited from std::exception

.SH Member functions

   destructor   destroys the exception object
   \fB[virtual]\fP    \fI(virtual public member function of std::exception)\fP
   what         returns an explanatory string
   \fB[virtual]\fP    \fI(virtual public member function of std::exception)\fP

.SH Example

   Compiles only in C++14 (or earlier) mode.


// Run this code

 #include <exception>
 #include <iostream>
 #include <stdexcept>

 void my_unexp()
 {
     throw;
 }

 void test()
     throw(std::bad_exception) // Dynamic exception specifications
                               // are deprecated in C++11
 {
     throw std::runtime_error("test");
 }

 int main()
 {
     std::set_unexpected(my_unexp); // Deprecated in C++11, removed in C++17

     try
     {
         test();
     }
     catch (const std::bad_exception& e)
     {
         std::cerr << "Caught " << e.what() << '\\n';
     }
 }

.SH Possible output:

 Caught std::bad_exception
